﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_BLL;
using V5_Model;

namespace V5_EShop.Admin
{
    public partial class Admin_ArticleClass : BasicPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Action = Input.SetString(Request["Action"]);
                Bind_DropClassList();
                switch (Action.Trim().ToLower())
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            this.gvList.DataSource = new BLLArticleClass().GetClassList(0);
            this.gvList.DataBind();
        }

        private void Bind_DropClassList()
        {
            this.ddlParentID.DataSource = new BLLArticleClass().GetDropDownList("(作为一级分类)", 0);
            this.ddlParentID.DataTextField = "ClassName";
            this.ddlParentID.DataValueField = "ClassID";
            this.ddlParentID.DataBind();
        }

        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelArticleClass model = new BLLArticleClass().GetModel(id);
            if (model != null)
            {
                this.ddlParentID.Enabled = false;
                this.txtClassName.Text = model.ClassName;
                this.ddlParentID.SelectedValue = model.ParentID.ToString();
                this.txtClassdescription.Text = model.ClassDescription;
                this.txtClassKeywords.Text = model.ClassKeywords;
                this.txtReadMe.Text = model.ReadMe;
                this.lblTitle.Text = "修改分类";
                this.txtClassID.Value = model.ClassID.ToString();
                this.btnSave.Text = "保存修改";
            }
        }
        private void Bind_AddSub(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            this.txtClassName.Text = "";
            this.txtClassdescription.Text = "";
            this.txtClassKeywords.Text = "";
            this.txtReadMe.Text = "";
            this.lblTitle.Text = "添加分类";
            this.txtClassID.Value = "0";
            this.btnSave.Text = "添加修改";
            this.ddlParentID.SelectedValue = id.ToString();
        }

        private void Bind_Delete(int id)
        {
            new BLLArticleClass().Delete(id);
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = e.CommandName.ToLower();
            int id = Input.SetInt(e.CommandArgument.ToString());
            switch (eName)
            {
                case "addsub":
                    Bind_AddSub(id);
                    break;
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLLArticleClass bll = new BLLArticleClass();
            ModelArticleClass model = new ModelArticleClass();
            int ChannelID = 0;
            int ClassID = Input.SetInt(this.txtClassID.Value);
            string ClassName = this.txtClassName.Text;
            string ReadMe = this.txtReadMe.Text;
            string ClassDescription = this.txtClassdescription.Text;
            string ClassKeywords = this.txtClassKeywords.Text;
            int ParentID = Input.SetInt(this.ddlParentID.SelectedValue);
            model.ChannelID = ChannelID;
            model.ClassID = ClassID;
            model.ClassName = ClassName;
            model.ParentID = ParentID;
            model.ClassDescription = ClassDescription;
            model.ClassKeywords = ClassKeywords;
            model.ReadMe = ReadMe;
            model.AddDateTime = DateTime.Now;
            if (ClassID == 0)
            {
                bll.Add(model,true);
            }
            else if (ClassID > 0)
            {
                //bll.Update(model);
                ModelArticleClass model1 = bll.GetModel(ClassID);
                model1.ChannelID = ChannelID;
                model1.ClassID = ClassID;
                model1.ClassName = ClassName;
                model1.ParentID = ParentID;
                model1.ClassDescription = ClassDescription;
                model1.ClassKeywords = ClassKeywords;
                model1.ReadMe = ReadMe;
                bll.Update(model1);
            }
            Bind_gvList();
        }
    }
}
